package com.csu.letaobackend.serviceM.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csu.letaobackend.bean.Shop;
import com.csu.letaobackend.bean.User;
import com.csu.letaobackend.commom.CommonResponse;
import com.csu.letaobackend.persistence.ShopDao;
import com.csu.letaobackend.serviceM.ShopMService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@MapperScan("com.csu.letaobackend.persistence")
public class ShopMServiceImpl implements ShopMService {

      @Autowired
      ShopDao shopDao;

      @Override
      public CommonResponse<IPage<Shop>> getAllShops(int pageid, int pagesize) {
            IPage<Shop> shopIPage = new Page<>(pageid, pagesize);
            shopDao.selectPage(shopIPage, null);

            return CommonResponse.createForSuccess(shopIPage);
      }

      @Override
      public CommonResponse<IPage<Shop>> searchShop(int pageid, int pagesize, String shopname, String sellername) {
            IPage<Shop> shopIPage = new Page<>(pageid, pagesize);
            QueryWrapper<Shop> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("name", shopname).like("sellername", sellername);
            shopDao.selectPage(shopIPage, queryWrapper);

            return CommonResponse.createForSuccess(shopIPage);
      }

      @Override
      public CommonResponse updateAvailable(int shopid, int available) {
            UpdateWrapper<Shop> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("shopid", shopid).set("available", available);

            shopDao.update(null,updateWrapper);
            return CommonResponse.createForSuccess("更新状态成功");
      }

      @Override
      public CommonResponse deleteShop(int shopid) {
            Shop shop = shopDao.selectById(shopid);
            if (shop == null)
                  return CommonResponse.createForError("店铺不存在");
            else
                  shopDao.deleteById(shopid);
            return CommonResponse.createForSuccessMessage("删除成功");
      }
}
